<?php
class Roles extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->Model(array("Mroles"));
		$this->breadcrumbs->push('Roles', 'admincp/roles/view');
	}
	
	public function view(){
        $data['icon_page']=$this->config->item('icon_view');
        $data['title']='Roles Manager';
        $data['sub_title']='Roles Manager';
        $data['columns'] = array(
        	'id' => trans('id'),
        	'name' => trans('title'),
        	'description' => trans('description')
        );
        $data['roles']=$this->Mroles->listRoles(array_keys($data['columns']));
		$this->parser->parse("roles/view.tpl", NULL, $data);
	}

	public function add(){
		$data['icon_page']=$this->config->item('icon_create');
        $data['title']='Create New Roles';
        $data['sub_title']='Create New Roles';
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$data_insert = array(
				'name' => $this->input->post('name'),
				'description' => $this->input->post('description')
			);
			$this->Mroles->insert($data_insert);
			$this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
			redirect(admincp_url()."roles/view");		
		}
		$this->breadcrumbs->push('Create Roles', 'admincp/roles/add');
		$this->parser->parse("roles/add.tpl", NULL, $data);
	}

	public function edit(){
		$data['icon_page']=$this->config->item('icon_edit');
        $data['title']='Edit Roles';
        $data['sub_title']='Edit Roles';
		$cache_id = $id = $this->uri->segment(4);
		$data['info'] = $this->Mroles->get_data($id);
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$data_update = array(
				'name' => $this->input->post('name'),
				'description' => $this->input->post('description') 
			);
			$this->Mroles->update($data_update,$id);
			$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
			redirect(admincp_url()."roles/view");
		}
		$this->breadcrumbs->push('Edit Roles', 'admincp/roles/edit/'.$id);
		$this->parser->parse("roles/edit.tpl", $cache_id, $data);
	}

	public function delete(){
		$id = $this->uri->segment(4);
		$this->Mroles->delete($id);
		$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		redirect(admincp_url()."roles/view");
	}

	public function privilege(){
		$data['icon_page']=$this->config->item('icon_privilege');
        $data['title']='Edit Privilege';
        $data['sub_title']='Edit Privilege';
		$cache_id = $id = $this->uri->segment(4);
		$data['privileges'] = $this->Mroles->get_privilege($id);
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$data_update = $this->input->post('privilege');
			$this->Mroles->update_role_permission($data_update,$id);
			$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
			redirect(admincp_url()."roles/view");
		}
		$this->breadcrumbs->push('Edit Privilege', 'admincp/privilege/edit/'.$id);
		$this->parser->parse("roles/privilege.tpl", $cache_id, $data);	
	}
	
}